package com.onlinelearn.controllers;

import com.onlinelearn.base.exception.ValidationGroups;
import com.onlinelearn.base.model.PageParams;
import com.onlinelearn.base.model.PageResult;
import com.onlinelearn.content.model.dto.AddCourseDto;
import com.onlinelearn.content.model.dto.CourseBaseInfoDto;
import com.onlinelearn.content.model.dto.EditCourseDto;
import com.onlinelearn.content.model.dto.QueryCourseParamsDto;
import com.onlinelearn.content.model.po.CourseBase;
import com.onlinelearn.content.service.CourseBaseInfoService;
import com.onlinelearn.content.service.impl.CourseMarketServiceImpl;
import com.onlinelearn.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "课程内容相关接口value",tags = "课程内容相关接口tag")
@RestController
public class ContenController {

    private static final Logger log = LoggerFactory.getLogger(ContenController.class);
    @Autowired
    CourseBaseInfoService courseBaseInfoService;

    @Autowired
    CourseMarketServiceImpl courseMarketService;


    @ApiOperation(value = "根据分页获取课程内容")
    @PreAuthorize("hasAnyAuthority('xc_teachmanager_course_list')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams, @RequestBody QueryCourseParamsDto queryCourseParamsDto){
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //机构id
        String companyId = user.getCompanyId();
        return courseBaseInfoService.queryCourseBaseList(Long.parseLong(companyId),pageParams,queryCourseParamsDto);
    }

    @ApiOperation(value = "新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated({ValidationGroups.Inster.class}) AddCourseDto addCourseDto){
        Long companyId = 22L;
        return courseBaseInfoService.createCourseBase(companyId,addCourseDto);
    }

    @ApiOperation("根据课程id查询课程基础信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        //取出当前用户身份
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);
        return courseBaseInfoService.getCourseBaseInfo(courseId);
    }

    @ApiOperation("修改课程基础信息")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto editCourseDto){
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        return courseBaseInfoService.updateCourseBase(companyId,editCourseDto);
    }

    @ApiOperation("删除课程信息")
    @DeleteMapping("/course/{courseId}")
    public void deleteCourseBase(@PathVariable Long courseId){
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        courseBaseInfoService.deleteCourseBase(companyId,courseId);
    }
}
